package com.nuc.workflowserver.controller;

import com.github.pagehelper.PageInfo;
import com.nuc.workflowserver.common.model.ResultModel;
import com.nuc.workflowserver.model.dto.PageDTO;
import com.nuc.workflowserver.model.pojo.Flow;
import com.nuc.workflowserver.model.pojo.User;
import com.nuc.workflowserver.model.vo.FlowVO;
import com.nuc.workflowserver.service.IFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("flow")
@Api(value = "生产线接口", description = "提供生产线的增删改查")
public class FlowController {

    @Autowired
    IFlowService iFlowService;

    @GetMapping("flowSel")
    @ApiOperation("获取生产线下拉列表")
    public ResultModel selectFlow() {
        List<Flow> flows = iFlowService.selectList(new Flow());
        return new ResultModel(flows, true);
    }

    @PostMapping("insertOrUpdateFlow")
    @ApiOperation("新增或者更新生产线")
    public ResultModel insertOrUpdateFlow(@RequestBody Flow flow,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            if (null != flow && null != flow.getId() && !""
                    .equals(flow.getId())) {
                flow.setUpdateUser(user.getId());
            } else {
                flow.setCreateUser(user.getId());
                flow.setUpdateUser(user.getId());
            }
        } else {
            return new ResultModel("请先登录！", false);
        }
        Integer res = iFlowService.insertOrUpdateFlow(flow);
        if (res > 0) {
            return new ResultModel("操作成功！", true);
        } else {
            return new ResultModel("操作失败！请联系管理员！", false);
        }
    }

    @PostMapping("selectFlowVOPage")
    @ApiOperation("分页查询生产线列表")
    public ResultModel selectFlowVOPage(@RequestBody PageDTO pageDTO) {
        PageInfo<FlowVO> page = iFlowService
                .selectFlowVOPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new ResultModel(page, true);
    }
}
